package com.xq.pethome.system.verifycode.controller;

import com.xq.pethome.utils.AjaxResult;
import com.xq.pethome.utils.StrUtils;
import com.xq.pethome.utils.VerifyCodeUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/vcode")
//这里是说明你这个类是干什么用的
@Api(value = "验证码的API",description="验证码的功能")
public class VerifyCodeController {
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/imgCode/{key}")
    public AjaxResult getImgCode(@PathVariable String key){
        try {
            //生成随机验证码
            String code = StrUtils.getRandomString(6);
            //存入redis。有效期3分钟
            redisTemplate.opsForValue().set(key,code,3, TimeUnit.MINUTES);
            String base64Img = VerifyCodeUtils.VerifyCode(110, 40, code);
            return AjaxResult.success().setData(base64Img);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络异常，请联系管理员");
        }
    }
}
